//
//  FirCirCommentdb.m
//  tcpoctest
//
//  Created by gump on 4/16/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "FirCirCommentdb.h"
#import "FirCirCommentDataSource.h"
#import "FirCirCommentInfo.h"

@implementation FirCirCommentdb

-(void)creatTable
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    [db executeUpdate:@"create table if not exists fircircomment(mail text,\
newsid text,speaker text,speakto text,strtime text,stindex char(1),commentid text,\
content text)"];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)initData
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    [[DataControl shareControl].fccommentDS RemoveAllCommentDic];
    NSString *strsql = [NSString stringWithFormat:@"SELECT * FROM fircircomment"];
    FMResultSet *s = [db executeQuery:strsql];
        
        while ([s next]) {
            FirCirCommentInfo *fccinfo = [[FirCirCommentInfo alloc] init];
            fccinfo.mail = [s stringForColumn:@"mail"];
            fccinfo.newsID = [s stringForColumn:@"newsid"];
            fccinfo.speaker = [s stringForColumn:@"speaker"];
            fccinfo.speakto = [s stringForColumn:@"speakto"];
            fccinfo.strtime = [s stringForColumn:@"strtime"];
            fccinfo.stIndex = [s intForColumn:@"stindex"];
            fccinfo.commentID = [s stringForColumn:@"commentid"];
            fccinfo.content = [s stringForColumn:@"content"];
            [[DataControl shareControl].fccommentDS SetCommentDic:fccinfo.commentID withObject:fccinfo];
        }
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)insertComment:(FirCirCommentInfo *)fccinfo
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    NSString *strsql = [NSString stringWithFormat:@"select * from fircircomment \
where commentID = '%@'",fccinfo.commentID];
    FMResultSet *s = [db executeQuery:strsql];
    if (![s next]) {
        
        strsql = [NSString stringWithFormat:@"insert into fircircomment \
(mail,newsid,speaker,speakto,strtime,stindex,commentid,content) \
values('%@','%@','%@','%@','%@','%d','%@','%@')"
                  ,fccinfo.mail
                  ,fccinfo.newsID
                  ,fccinfo.speaker
                  ,fccinfo.speakto
                  ,fccinfo.strtime
                  ,fccinfo.stIndex
                  ,fccinfo.commentID
                  ,fccinfo.content];
        
        [db executeUpdate:strsql];
    }
    
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)delCommentByID:(NSString *)commentid
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    NSString *strsql = [NSString stringWithFormat:@"DELETE FROM fircircomment WHERE commentid='%@'"
                        ,commentid];
    
    [db executeUpdate:strsql];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)delCommentByNewsID:(NSString *)newid
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    NSString *strsql = [NSString stringWithFormat:@"DELETE FROM fircircomment WHERE newsid='%@'"
                        ,newid];
    
    [db executeUpdate:strsql];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

@end
